﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace Ubiquity.Core.Extend
{
    /// <summary>
    /// 枚举Model
    /// </summary>
    public class EnumModel
    {
        /// <summary>
        /// 值 
        /// </summary>
        public int Value { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 显示描述
        /// </summary>
        public string Text { get; set; }
    }
    /// <summary>
    /// 枚举帮助类 [Description("")]
    /// </summary>
    public static class EnumHelper
    {
        /// <summary> 
        /// 获得枚举类型数据项（不包括空项）
        /// </summary> 
        /// <param name="enumType">枚举类型</param> 
        /// <returns></returns> 
        public static List<EnumModel> GetItems(this Type enumType)
        {
            if (!enumType.IsEnum)
                throw new InvalidOperationException();

            List<EnumModel> list = new List<EnumModel>();

            // 获取Description特性 
            Type typeDescription = typeof(DescriptionAttribute);
            // 获取枚举字段
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                if (!field.FieldType.IsEnum)
                    continue;

                // 获取枚举值
                int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
                string code = enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null).ToString();
                // 不包括空项
                if (value >= -1)
                {
                    object[] array = field.GetCustomAttributes(typeDescription, false);
                    var text = array.Length > 0 ? ((DescriptionAttribute)array[0]).Description : field.Name;
                    //添加到列表
                    list.Add(new EnumModel() { Value = value, Text = text, Code = code });
                }
            }
            return list;
        }

        /// <summary>
        /// 得到text
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static string GetText(this Enum enumType)
        {
            List<EnumModel> enumModelS = GetItems(enumType.GetType());
            EnumModel model = enumModelS.FirstOrDefault(m => m.Code == enumType.ToString());

            return model != null ? model.Text : "";

        }

        public static Dictionary<int, string> EnumToDictionary<T>(this T t)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            if (!typeof(T).IsEnum)
            {
                return dic;
            }
            string desc = string.Empty;
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                var attrs = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attrs.Length > 0)
                {
                    DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
                    desc = descAttr != null ? descAttr.Description : "";
                }
                dic.Add(Convert.ToInt32(item), desc);
            }
            return dic;
        }
    }
}
